package com.moonshineproject.plugin.actionscript.as3parser
{
	import com.moonshineproject.editor.BasicTextEditor;
	import com.moonshineproject.plugin.IMenuPlugin;
	import com.moonshineproject.plugin.PluginBase;
	import com.moonshineproject.plugin.actionscript.as3parser.listener.ParserController;
	import com.moonshineproject.plugin.actionscript.as3parser.swcparser.SWCDataSerializer;
	import com.moonshineproject.plugin.actionscript.as3project.vo.AS3ProjectVO;
	import com.moonshineproject.plugin.menu.vo.MenuItem;
	import com.moonshineproject.plugin.project.event.ProjectEvent;
	import com.moonshineproject.plugin.settings.ISettingsProvider;
	import com.moonshineproject.plugin.settings.vo.ISetting;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.geom.Point;
	import flash.ui.Keyboard;
	
	import mx.collections.ArrayCollection;

	public class AS3ParserPlugin extends PluginBase implements IMenuPlugin, ISettingsProvider
	{
		public static const PONY_COMPLETE:String = "ponyCompleteEvent";


		override public function get name():String { return "AS3 Autocompletion"; }
		override public function get author():String { return "Moonshine Project Team"; }
		override public function get description():String { return "Provides code completation for Flash/Flex projects."; }

		override public function activate():void
		{
			super.activate();
			
			dispatcher.addEventListener(PONY_COMPLETE, handlePonyComplete);
			dispatcher.addEventListener(ProjectEvent.ADD_PROJECT, handleProjectOpen);
			registerCommand("debug-clear-completion-swc-cache", clearSWCCache);
		}

		override public function deactivate():void
		{
			super.deactivate();

			dispatcher.removeEventListener(PONY_COMPLETE, handlePonyComplete);
			dispatcher.removeEventListener(ProjectEvent.ADD_PROJECT, handleProjectOpen);
			unregisterCommand("debug-clear-completion-swc-cache");
		}

		public function getSettingsList():Vector.<ISetting>
		{
			return Vector.<ISetting>([]);
		}

		public function getMenu():MenuItem
		{
			return new MenuItem("AS3 Parser", [
				new MenuItem("Ponycomplete", null, PONY_COMPLETE,
				' ', [Keyboard.CONTROL],
				' ', [Keyboard.CONTROL])
				]);
		}

		protected function handleProjectOpen(event:ProjectEvent):void
		{
			var p:AS3ProjectVO = event.project as AS3ProjectVO;
			if (p)
			{
				p.parserController = new ParserController(p);
			}
		}

		protected function handlePonyComplete(event:Event):void
		{
			var editor:BasicTextEditor = model.activeEditor as BasicTextEditor;
			if (editor)
			{
				var file:File = BasicTextEditor(model.activeEditor).currentFile;
				// Don't show for non-as files
				if (!file)
					return;
				if (!(file.extension == "as"
					|| file.extension == "mxml"))
					return;

				var p:AS3ProjectVO = model.activeProject as AS3ProjectVO;

				if (!p)
				{
					error("Autocompletion only works on projects for now, sorry.")
					return;
				}
				
				var lineChar:Point = editor.getEditorComponent().getLineCharIndex();
				var finder:NodeFinder = new NodeFinder(p);
				finder.charIdx = editor.getEditorComponent().getCaretIndex();
				finder.lineIdx = lineChar.y;
				finder.caretColumnIdx = lineChar.x;
				
				var completions:ArrayCollection = finder.find(
					editor.getEditorComponent().getLines(),
					editor.currentFile.nativePath,
					model.activeProject.folderPath
				);
				 
				editor.getEditorComponent().showCompletion(completions, finder.completionContext);
			}
		}
		
		protected function clearSWCCache(args:Array):void
		{
			var dir:File = SWCDataSerializer.getCacheDir();
			dir.deleteDirectory(true);
			
			print("All SWCs cached for autocompletion removed. Re-open projects to parse them again.");
		}

	}
}